package com.hxkcloud.patient.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.activitys.CommentActivity;
import com.hxkcloud.patient.beans.ArticleBean;
import com.hxkcloud.patient.utils.ImageOptionUtils;
import com.hxkcloud.patient.utils.ImageUtil;
import com.hxkcloud.patient.views.RoundImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by ray_lei on 2016/7/6.
 */
public class NewsListAdapter extends RecyclerView.Adapter<ViewHolder> {


    private Context mContext;
    private List<ArticleBean> datas;
    private RecyclerView recyclerview;
    private LayoutInflater mInflater;

    private ResultSelectedListener listener;

    protected ImageLoader imageLoader;
    private int screenWidth;

    private String type = "";

    public NewsListAdapter(Context mContext, List<ArticleBean> datas,
                           RecyclerView recyclerview, ResultSelectedListener listener, int width, String type) {
        this.mContext = mContext;
        this.datas = datas;
        this.recyclerview = recyclerview;
        this.listener = listener;
        this.screenWidth = width;
        this.type = type;
        mInflater = LayoutInflater.from(mContext);
        imageLoader = ImageLoader.getInstance();
    }

    public void setData(List<ArticleBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public void clearData() {
        datas.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        // TODO Auto-generated method stub
        return datas.size();
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        // TODO Auto-generated method stub


        Holder mHolder = (Holder) holder;

        LinearLayout.LayoutParams lp_head = new LinearLayout.LayoutParams(screenWidth / 6, screenWidth / 6);
        lp_head.setMargins(20, 0, 0, 0);
        mHolder.icon.setLayoutParams(lp_head);
        imageLoader.displayImage(datas.get(position).getImg(),
                mHolder.icon, ImageOptionUtils.getInstance().getArticlePicSmallOptions(ImageUtil.dip2px(5, mContext)));
        if(datas.get(position).getName().length()>30){
            mHolder.titleTV.setText(datas.get(position).getName()+"...");
        }else{
            mHolder.titleTV.setText(datas.get(position).getName());
        }
        mHolder.titleTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, CommentActivity.class);
                mContext.startActivity(intent);
            }
        });
        mHolder.timeTV.setText(datas.get(position).getCreateTime());
        if ("0".equals(type)) {
            mHolder.timeTV.setVisibility(View.VISIBLE);
        } else {
            mHolder.timeTV.setVisibility(View.GONE);
        }
        if (datas.size() - position == 1) {
            mHolder.itemView.setVisibility(View.INVISIBLE);
        } else {
            mHolder.itemView.setVisibility(View.VISIBLE);
        }
        if(datas.get(position).getIntro().length()>50){
            mHolder.descriptionTV.setText(datas.get(position).getIntro()+"...");
        }else{
            mHolder.descriptionTV.setText(datas.get(position).getIntro());
        }
        mHolder.layout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                listener.onSelected("click", datas.get(position));

            }

        });


    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        View v = mInflater.inflate(R.layout.item_news_layout, null);
        ViewHolder holer = new Holder(v);

        return holer;
    }

    @Override
    public int getItemViewType(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    /*
     * Holder
     */
    public class Holder extends ViewHolder {

        private LinearLayout layout;
        private RoundImageView icon;
        private TextView titleTV;
        private TextView timeTV;
        private TextView descriptionTV;
        private View itemView;

        public Holder(View view) {
            super(view);
            layout = (LinearLayout) view.findViewById(R.id.news_layout_whole);
            icon = (RoundImageView) view.findViewById(R.id.news_item_ci_icon);
            titleTV = (TextView) view.findViewById(R.id.news_item_tv_title);
            timeTV = (TextView) view.findViewById(R.id.news_item_tv_time);
            descriptionTV = (TextView) view.findViewById(R.id.news_item_tv_description);
            itemView = (View) view.findViewById(R.id.news_item_view);

        }
    }

    public interface ResultSelectedListener {
        void onSelected(String type, ArticleBean info);
    }


}
